const query = require('../db');
const util = require('../util');
module.exports = {
    //添加答题卡
    addAnwser(data,callback){
        let PK_AnwserCardGuid = util.createGuid();
        let CreateTime = util.getNowTime();
        let UserName = data.userName;
        let sql;
        if(UserName) {
        sql = `INSERT INTO anwsercard(PK_AnwserCardGuid,FK_TrainingGuid,UserName,\`Status\`,CreateTime) VALUES('${PK_AnwserCardGuid}','${data.FK_TrainingGuid}','${UserName}',1,${CreateTime})`
        }
        else {
        sql = `INSERT INTO anwsercard(PK_AnwserCardGuid,FK_TrainingGuid,FK_UserGuid,\`Status\`,CreateTime) VALUES('${PK_AnwserCardGuid}','${data.FK_TrainingGuid}','${data.FK_UserGuid}',1,${CreateTime})`;
    }
        query(sql,(err,data)=>{
            if(err){
                  util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                callback(PK_AnwserCardGuid);
            }
        })
    },
    //根据答题卡ID获取答题卡信息,status:0代表已删除，1代表未删除，默认为1
    getAnwserOfID(id,status,callback){
        if(typeof(status)=='undefined') status=1;
        let sql = `SELECT * FROM anwsercard WHERE PK_AnwserCardGuid = '${id}' AND \`Status\` =${status}`;
        query(sql,(err,data)=>{
               if(err){
                util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                callback(data);
            }
        })
    },
    //根据答题卡ID更新答题卡
    updateAnwser(data,callback){
        let sql = `UPDATE anwsercard SET Content = '${data.Content}',Score = '${data.Score}' WHERE PK_AnwserCardGuid = '${data.id}' `;
        let id = data.id;
        query(sql,(err,data)=>{
            if(err){
                util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                this.getAnwserOfID(id,1,callback);
            }
        })
    },
    //根据测试ID和用户ID获取答题卡,status:0代表已删除，1代表未删除，默认为1
    getANwserOfTraining(tariningID,userID,status,callback){
            if(typeof(status)=='undefined') status=1;
            let sql = `SELECT * FROM anwsercard WHERE status =${status} AND `;
            if(tariningID&&userID) sql+=` FK_TrainingGuid='${tariningID}' AND FK_UserGuid='${userID}'`;
            else if(tariningID) sql+=` FK_TrainingGuid='${tariningID}'`;
            else sql+=` FK_UserGuid='${userID}'`;
            query(sql,(err,data)=>{
                if(err){
                util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                callback(data);
            }
            })
    },
    //根据训练ID删除此次训练的答题卡
    delAnwser(id,callback){
        let sql = `UPDATE anwsercard SET \`Status\`=0 WHERE FK_TrainingGuid = '${id}'`;
        query(sql,(err,data)=>{
            if(err){
                  util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                 this.getAnwserOfID(id,0,callback);
            }
        })
    },
    //更新答题卡完成状态根据答题卡ID
    upDateFinishByID(id,callback){
        let sql = `UPDATE anwsercard SET finishStatus = 1 WHERE PK_AnwserCardGuid =  '${id}'`;
        query(sql,(err,data)=>{
            if(err){
                util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                this.getAnwserOfID(id,1,callback);
            }
        })
    },
    //根据训练ID获取所有已经完成的答题卡
    getAnswerFinishByTrainingId(id,callback){
        let sql = `SELECT * FROM anwsercard WHERE FK_TrainingGuid ='${id}' AND finishStatus =1`;
        query(sql,(err,data)=>{
             if(err){
                 util.writeErr(err+',sql='+sql+'时间='+new Date+',');
             }else{
                 callback(data);
             }
        })
    }
}